import java.awt.*;
import java.util.*;

class Student implements Comparable<Student>{
    int age;
    String name;

    public Student(String name, int age) {
        this.age = age;
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age && Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(age, name);
    }

    @Override
    public int compareTo(Student o) {
        //return this.age - o.age;
        return o.age - this.age;//大根堆
    }
}
class NameCompartor implements Comparator<Student> {

    @Override
    public int compare(Student o1,Student o2) {
        return o1.name.compareTo(o2.name);
    }
}
class AgeCompartor implements Comparator<Student> {

    @Override
    public int compare(Student o1,Student o2) {
        return o2.age - o1.age;
    }
}
class Incmp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o1.compareTo(o2);//小跟堆
        //return o2.compareTo(o1);//大跟堆

    }
}

public class Test {
    public static void main(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initElem(array);
        testHeap.creatHeap();
        testHeap.heapSort();
        System.out.println();
    }



    public static void main5(String[] args) {
        AgeCompartor ageCompartor = new AgeCompartor();
        Queue<Student> priorityQueue2 = new PriorityQueue<>(ageCompartor);
        Student student1 = new Student("zhangsan",27);
        Student student2 = new Student("lisi",15);

        System.out.println("======");

        /*Incmp incmp = new Incmp();
        Queue<Integer> priorityQueue1 = new PriorityQueue<>(incmp);*/
        /*
        //匿名内部类
        Queue<Integer> priorityQueue1 = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
         */
        Queue<Integer> priorityQueue1 = new PriorityQueue<>
                ((o1,o2)->{return o2.compareTo(o1);});//lambda表达式
        priorityQueue1.offer(1);
        priorityQueue1.offer(2);

    }
    public static void main4(String[] args) {
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("zhangsan",20);

        System.out.println(student1 == student2);
        System.out.println(student1.equals(student2));

        System.out.println(student1.compareTo(student2));

        NameCompartor nameCompartor = new NameCompartor();
        int ret = nameCompartor.compare(student1,student2);
        System.out.println(ret);

        AgeCompartor ageCompartor = new AgeCompartor();
        int ret2 = ageCompartor.compare(student1,student2);
        System.out.println(ret2);
    }
    //最小k的个数
    public int[] smallestK(int[] arr, int k) {
        int[] ret = new int[k];
        if(arr == null || k == 0) {
            return ret;
        }
        Queue<Integer> minHeap = new PriorityQueue<>(arr.length);

        for(int x : arr) {
            minHeap.offer(x);
        }
        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;

    }
    //前K个最大的元素
    //时间复杂度  N*logK
    public static int[] maxK(int[] arr, int k) {
        int[] ret = new int[k];
        if(arr == null || k == 0) {
            return ret;
        }
        Queue<Integer> minHeap = new PriorityQueue<>(k);

        //1、遍历数组的前K个 放到堆当中 K*logK
        for (int i = 0; i < k; i++) {
            minHeap.offer(arr[i]);
        }
        //2.遍历剩下的K-1个，每次和堆顶元素进行比较 (N-K)logK
        for (int i = k; i < arr.length; i++) {
            //堆顶元素 小的时候，就出堆
            if(arr[i] > minHeap.peek()) {
                minHeap.poll();
                minHeap.offer(arr[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = minHeap.poll();
        }
        return ret;
    }

    public static void main3(String[] args) {
        int[] array = {1,5,43,3,2,7,98,41,567,67};
        int[] ret = maxK(array,3);
        System.out.println(Arrays.toString(ret));
    }

    public static void main2(String[] args) {
        Queue<Integer> priorityQueue1 = new PriorityQueue<>();
        priorityQueue1.offer(10);
        priorityQueue1.offer(2);
        System.out.println(priorityQueue1.poll());


    }
    public static void main1(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = {27,15,19,18,28,34,65,49,25,37};
        testHeap.initElem(array);
        testHeap.creatHeap();
        System.out.println();
        //testHeap.offer(80);
        testHeap.pop();
    }
}

